package wx.core
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 
	 * @author wujianqi
	 * 
	 * 主控件 基类
	 * 
	 */	
	public class Control extends Sprite implements IControl
	{
		protected var _enable:Boolean = true;
		private var _skins:Vector.<DisplayObject>;
		
		public function Control()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE,onLoaded);
		}		
		
		
		/**
		 * 兼容皮肤ScaleBitmapSprite等 宽、高处理
		 */
		protected function validSkinItem(value:DisplayObject):void{
			if(_skins==null) _skins = new <DisplayObject>[];
			if(_skins.indexOf(value)==-1) _skins.push(value);
		}
		override public function set width(value:Number):void{
			if(this.width != value && _skins!=null){
				for each (var i:DisplayObject in _skins) i.width = value;
			}else super.width = value;
		}
		override public function set height(value:Number):void{
			if(this.height != value && _skins!=null){
				for each (var i:DisplayObject in _skins) i.height = value;
			}else super.height = value;
		} 
		
		 
		/**
		 * 公共属性，组件状态
		 */		
		public function get enable():Boolean
		{
			return _enable;
		}
		public function set enable(value:Boolean):void
		{
			_enable = value;
		}		
		
		/**
		 * 禁用状态，监听事件无效
		 */	
		override public function addEventListener(type:String, listener:Function, 
												  useCapture:Boolean=false, priority:int=0, 
												  useWeakReference:Boolean=false):void{
			if(_enable) super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		
		}		

		/**
		 * 组件移除gc处理
		 */	
		protected function dispose():void {}
		private function onLoaded(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onLoaded);			
			addEventListener(Event.REMOVED_FROM_STAGE,function():void{				
				removeEventListener(Event.REMOVED_FROM_STAGE,arguments.callee);
				dispose();
				_skins = null;
			});
		}
			
		
	}
}